/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Arrays;
import javax.swing.JComponent;
import javax.swing.JTextField;
import model.Base;
import model.Book;
import model.CD;
import model.DVD;
import model.Document;
import model.Magazine;
import model.NewsPaper;
import view.NewDocumentWindow;

/**
 *
 * @author Pierre-Antoine
 */
public class NewDocumentController implements ActionListener, ItemListener {

    private NewDocumentWindow view;
    private Base model;

    public NewDocumentController(Base model, NewDocumentWindow view) {
		this.model = model;
		this.view = view;

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String btn = ((JComponent)e.getSource()).getName();
		if(btn.equals(NewDocumentWindow.BTN_CANCEL)) {
			this.view.dispose();
		}
		else if(btn.equals(NewDocumentWindow.BTN_CREATE)) {
			Document doc = null;
			String type = this.view.getCbbCategory();
			if(type.equals("Livre")) {
				try {
					doc = new Book(this.view.getDocumentTitle(), Short.parseShort(this.view.getDocumentYear()), this.view.getTxtTypeBook(), Arrays.asList(this.view.getDocumentAuthors().split(", ")), this.view.getDocumentEditor(), Integer.parseInt(this.view.getTxtNbPageBook()));
				}
				catch(Exception ex) {
					this.view.setInfo("Erreur à la création", true);
					return;
				}
			}
			else if(type.equals("CD")) {
				try {
					doc = new CD(this.view.getDocumentTitle(), Short.parseShort(this.view.getDocumentYear()), this.view.getTxtTypeCD(), Arrays.asList(this.view.getDocumentAuthors().split(", ")), this.view.getDocumentEditor(), Integer.parseInt(this.view.getTxtLenghtCD()));
				}
				catch(Exception ex) {
					this.view.setInfo("Erreur à la création", true);
					return;
				}
			}
			else if(type.equals("DVD")) {
				try {
					doc = new DVD(this.view.getDocumentTitle(), Short.parseShort(this.view.getDocumentYear()), this.view.getTxtTypeDVD(), Arrays.asList(this.view.getDocumentAuthors().split(", ")), this.view.getDocumentEditor(), Integer.parseInt(this.view.getTxtLenghtDVD()));
				}
				catch(Exception ex) {
					this.view.setInfo("Erreur à la création", true);
					return;
				}
			}
			else if(type.equals("Magazine")) {
				try {
					doc = new Magazine(this.view.getDocumentTitle(), Short.parseShort(this.view.getDocumentYear()), Arrays.asList(this.view.getDocumentAuthors().split(", ")), this.view.getDocumentEditor(), Integer.parseInt(this.view.getTxtNbPageMag()));
				}
				catch(Exception ex) {
					this.view.setInfo("Erreur à la création", true);
					return;
				}
			}
			else if(type.equals("Journal")) {
				try {
					doc = new NewsPaper(this.view.getDocumentTitle(), Short.parseShort(this.view.getDocumentYear()), Arrays.asList(this.view.getDocumentAuthors().split(", ")), this.view.getDocumentEditor(), this.view.getTxtDayDate());
				}
				catch(Exception ex) {
					this.view.setInfo("Erreur à la création", true);
					return;
				}
			}
			this.model.addDocument(doc);
			this.view.setInfo("Document créer", false);
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		String s = (String)e.getItem();
		if(s.equals("Livre")) {
			this.view.setBookView();
		}
		else if(s.equals("CD")) {
			this.view.setCDView();
		}
		else if(s.equals("DVD")) {
			this.view.setDVDView();
		}
		else if(s.equals("Magazine")) {
			this.view.setMagView();
		}
		else if(s.equals("Journal")) {
			this.view.setNewsPaperView();
		}
	}
}
